/*
 *       - httpDownlaodServlet -
 *      use with httpDownload
 *
 *      Date: 08 oct 2004 - completed (no broken files download )
 *
 *
 *
 *
 * */
//package com.softplus.carrefour.rollout.frontend.servlet.systemutility;
package com.softplus.carrefour.hd.frontend.servlet.process;

import com.epro.standard.StandardException;
import com.epro.standard.frontend.datasetup.MessageManager;
import com.epro.standard.frontend.util.EproServletUtil;
import com.epro.standard.infrastructure.dataaccess.DBManage;

import com.softplus.carrefour.CFException;
import com.softplus.carrefour.hd.CFHDSystem;
import com.softplus.carrefour.hd.bltier.entity.CfhdCaseDocPK;
import com.softplus.carrefour.hd.bltier.entity.CfhdCaseHead;
import com.softplus.carrefour.hd.bltier.entity.UserAccount;
import com.softplus.carrefour.hd.bltier.pc.CCaseDocManage;
import com.softplus.carrefour.hd.bltier.pc.CCaseHeadManage;
import com.softplus.carrefour.hd.bltier.pc.ICaseHeadViewer;
import com.softplus.carrefour.hd.bltier.util.DateUtil;
//
//import com.softplus.carrefour.rollout.bltier.DOCUMENT_CONTAINERVO;
//import com.softplus.carrefour.rollout.bltier.util.httpDownload;

import oracle.sql.BLOB;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.math.BigDecimal;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.*;
import javax.servlet.http.*;


public class CFHDPC06 extends HttpServlet {
    private String CONTENT_TYPE = "text/html; charset=TIS-620";
    BufferedInputStream bis = null;
    InputStream is = null;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, 
                                                           IOException {

        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);

        System.out.println(" ======================= downloadServlet  =======================");
        String action = servletUtil.getRequestValue("action");
        System.out.println("======== action++++" + action);
        if (action.equals("DowLoad"))
            DowLoad(request, response);
        else if (action.equals("CloseCase"))
            CloseCase(request, response);
        else if (action.equals("UnlockCase"))
            UnlockCase(request, response);
    }


    //	-------------------------------------------------------------------------------------------------->
    //																																		  \\
    //														DowLoad                                                             \\
    //																																		  \\
    //	--------------------------------------------------------------------------------------------------->  

    public void DowLoad(HttpServletRequest request, 
                        HttpServletResponse response) throws ServletException, 
                                                             IOException {
        //	======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);


        //		  int index = new Integer(request.getParameter("index")).intValue();

        String caseId = servletUtil.getRequestValue("caseId");
        String detSeq = servletUtil.getRequestValue("detSeq");
        String docSeq = servletUtil.getRequestValue("docSeq");
        String attDesc = servletUtil.getRequestValue("attDesc");

        System.out.println("Case ID = " + caseId);
        System.out.println("Det Seq = " + detSeq);
        System.out.println("Doc Seq = " + docSeq);
        System.out.println("Att Desc = " + attDesc);


        CCaseDocManage manage = new CCaseDocManage();
        try {
            manage.downloadCaseDocFile(new CfhdCaseDocPK(caseId, 
                                                         new BigDecimal(detSeq), 
                                                         new BigDecimal(docSeq)), 
                                       response);
        } catch (CFException e) {
            e.printStackTrace();
        }

    }

    //	-------------------------------------------------------------------------------------------------->
    //																																		  \\
    //														CloseCase                                                             \\
    //																																		  \\
    //	--------------------------------------------------------------------------------------------------->  

    public void CloseCase(HttpServletRequest request, 
                          HttpServletResponse response) throws ServletException, 
                                                               IOException {
        //	======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);

        //			======================Define redirect page==================>
        String parameterLink = ""; //***********change
        String URL = 
            "/CFHD/servlet/com.softplus.carrefour.hd.frontend.servlet.process.CFHDPC01"; //fileInfoUtil.getRedirectFromServlet();
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;
        UserAccount userAcc = (UserAccount)session.getAttribute("userVO");
        String userID = userAcc.getUserId() == null ? "" : userAcc.getUserId();

        String caseId = servletUtil.getRequestValue("caseId");
        System.out.println("Case ID = " + caseId);

        ICaseHeadViewer caseheadManage = 
            (CCaseHeadManage)CFHDSystem.getControlClass("CASE_HEAD");

        CCaseHeadManage manage1 = new CCaseHeadManage();
        try {
            CfhdCaseHead caseheadEntity = 
                caseheadManage.getCaseHeadDesc(caseId);
            caseheadEntity.setCaseStatus(CfhdCaseHead.STATUS_CLOSE); //Update 25/12/2004  18.06  ���� CfhdCaseHead.STATUS_CLOSE
            caseheadEntity.setCloseCaseDate(DateUtil.getLocalDate(userID)); //Update 25/01/2005  18.06  
            manage1.updateCfhdCaseHead(caseheadEntity);
            session.setAttribute("hasError", 
                                 MessageManager.getClosecaseSuccessString());
            System.out.println("++++++++++successLink+++++++++ " + caseId);

            response.sendRedirect(successLink);

        } catch (CFException e) {
            e.printStackTrace();
            session.setAttribute("hasError", e.getDescription());
            response.sendRedirect(unsuccessLink);

        }

    }


    //	-------------------------------------------------------------------------------------------------->
    //																																			\\
    //														  UnlockCase                                                             \\
    //																																			\\
    //	  --------------------------------------------------------------------------------------------------->  

    public void UnlockCase(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        //	  ======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);
        //-----------------

        String caseId = servletUtil.getRequestValue("caseId");
        System.out.println("Case ID = " + caseId);


        //			  ======================Define redirect page==================>
        String parameterLink = 
            "&casecat=my&caseno=" + caseId; //***********change
        String URL = 
            "/CFHD/servlet/com.softplus.carrefour.hd.frontend.servlet.process.CFHDPC07_0";
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;

        ICaseHeadViewer caseheadManage = 
            (CCaseHeadManage)CFHDSystem.getControlClass("CASE_HEAD");

        CCaseHeadManage manage1 = new CCaseHeadManage();
        try {
            CfhdCaseHead caseheadEntity = 
                caseheadManage.getCaseHeadDesc(caseId);
            caseheadEntity.setOpenStatus(true);
            manage1.updateCfhdCaseHead(caseheadEntity);
            //  session.setAttribute("hasError",MessageManager.getClosecaseSuccessString());
            System.out.println("++++++++++successLink+++++++++ " + caseId);

            response.sendRedirect(successLink);

        } catch (CFException e) {
            e.printStackTrace();
            session.setAttribute("hasError", e.getDescription());
            response.sendRedirect(unsuccessLink);

        }

    }
    //	----------------------------------------------------------------------

}
